cmake_minimum_required(VERSION 3.8)
project(shared_parameters)

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(Boost REQUIRED COMPONENTS system)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(agv_srvs REQUIRED)

find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp REQUIRED)

find_library(TINYXML2_LIBRARY NAMES tinyxml2)
find_path(TINYXML2_INCLUDE_DIR NAMES tinyxml2.h)

if(NOT TINYXML2_LIBRARY OR NOT TINYXML2_INCLUDE_DIR)
    message(FATAL_ERROR "Could not find tinyxml2 library")
endif()


# Set C++ standards
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

# Include directories
include_directories(
  include
  ${Boost_INCLUDE_DIRS}
  ${TINYXML2_INCLUDE_DIR}
  ${JSONCPP_INCLUDE_DIRS}
)

# Create the shared library
add_library(${PROJECT_NAME} SHARED
  src/shared_parameters.cpp
)

# Link against Boost
target_link_libraries(${PROJECT_NAME}
  ${Boost_LIBRARIES}
  ${JSONCPP_LIBRARIES} 
)

add_executable(param_writer src/param_writer_node.cpp)
ament_target_dependencies(param_writer
  rclcpp
  std_msgs
)

target_link_libraries(param_writer
  shared_parameters
)

add_executable(param_reader src/param_reader_node.cpp)
ament_target_dependencies(param_reader
  rclcpp
  std_msgs
)

target_link_libraries(param_reader
  shared_parameters
)

add_executable(param_server src/param_server.cpp)
ament_target_dependencies(param_server
  rclcpp
  agv_srvs
)

target_link_libraries(param_server
  shared_parameters
  ${TINYXML2_LIBRARY}
)

# Install targets
install(
  DIRECTORY include/
  DESTINATION include
)

add_executable(stress_testing src/stress_testing.cpp)
install(
  TARGETS ${PROJECT_NAME}
  EXPORT ${PROJECT_NAME}
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include
)

install(TARGETS
  param_writer
  param_reader
  param_server
  stress_testing
  DESTINATION lib/${PROJECT_NAME}
)

install(FILES
  config/AGVParam.xml
  DESTINATION share/${PROJECT_NAME}/config
)

# Export dependencies
ament_export_targets(${PROJECT_NAME} HAS_LIBRARY_TARGET)
ament_export_dependencies(Boost)

ament_package()